package com.lxyk.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lxyk.pojo.User;
import com.lxyk.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 查询所有数据(报废)
     * @param model
     * @return
     */
    @RequestMapping("/findAll")
    public String findAll(Model model){
        List<User> list = userService.queryAll();
        model.addAttribute("list",list);
        return "list";  //视图
    }

    /**
     * 跳转到add.jsp页面
     * @return
     */
    @RequestMapping("/toAdd")
    public String toAdd(){
        return "add";  //视图
    }

    /**
     * 新增
     * @param user
     * @return
     */
    @RequestMapping("/add")
    public String add(User user){
        userService.addUser(user);
        return "redirect:/user/findByPage";  //添加成功重定向list.jsp
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @RequestMapping("/deleteById")
    public String deleteById(Integer id){
        userService.deleteUser(id);
        return "redirect:/user/findByPage";  //删除成功重定向list.jsp
    }

    /**
     * 根据id查询，跳转到update.jsp
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/findById")
    public String findById(Integer id,Model model){
        User user = userService.findUserById(id);
        model.addAttribute("user",user);
        return "update";  //跳转到update.jsp
    }

    /**
     * 修改
     * @param user
     * @return
     */
    @RequestMapping("/update")
    public String update(User user){
        userService.updateUser(user);
        return "redirect:/user/findByPage";  //修改成功重定向list.jsp
    }

    /**
     * 批量删除
     * @param uid
     * @return
     */
    @RequestMapping("/deleteCounts")
    public String deleteCounts(String[] uid){
        if (uid != null && uid.length > 0){
            //遍历数组
            for (String id : uid) {
                //调用删除接口
                userService.deleteUser(Integer.parseInt(id));
            }
        }
        return "redirect:/user/findByPage";  //删除成功重定向list.jsp
    }

    /**
     * 根据条件动态查询  (报废)
     * @param user
     * @param model
     * @return
     */
    @RequestMapping("/findByCode")
    public String findByCode(User user,Model model){
        //新建一个map集合，用于展示页面，数据回显
        HashMap<String,Object> map = new HashMap<String, Object>();
        map.put("name",user.getName());
        map.put("address",user.getAddress());
        map.put("email",user.getEmail());

        //调用动态查询接口
        List<User> code = userService.findByCode(user);

        model.addAttribute("list",code);
        model.addAttribute("map",map);
        return "list";
    }

    /**
     * 分页+条件查询
     * @param pageNum  当前页
     * @param pageSize 每页记录数
     * @param user
     * @param model
     * @return
     */
    @RequestMapping("/findByPage")
    public String findByPage(Integer pageNum,Integer pageSize,User user, Model model){
        if (pageNum ==null || pageNum < 1 ||"".equals(pageNum)){
            pageNum = 1;
        }
        if (pageSize == null || pageSize < 1 || "".equals(pageSize)){
            pageSize = 5;
        }

        //新建map集合  用于数据展示，回显
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("name",user.getName());
        map.put("address",user.getAddress());
        map.put("email",user.getEmail());

        //分页
        PageHelper.startPage(pageNum, pageSize);
        List<User> list = userService.findByCode(user);
        PageInfo<User> pageInfo = new PageInfo<User>(list);

        model.addAttribute("map",map);
        model.addAttribute("pageInfo",pageInfo);
        return "list";
    }
}
